package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageOtherMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageOtherOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherOverviewBean;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class OtherMaterielOverviewAdapter extends BaseAdapter {

    private Context mContext;
    private List<InStorageOtherOverviewBean.MaterielListBean> mMaterielList;
    private LayoutInflater inflater;
    private InStorageOtherOverviewActivity.InStorageOtherOverviewListener mInStorageOtherOverviewListener;
    private String mMaterielCode;

    public OtherMaterielOverviewAdapter(Context context, List<InStorageOtherOverviewBean.MaterielListBean> materielList, String materielCode, InStorageOtherOverviewActivity.InStorageOtherOverviewListener inStorageOtherMaterielOverviewListener) {
        mContext = context;
        mMaterielList = materielList;
        mMaterielCode = materielCode;
        mInStorageOtherOverviewListener = inStorageOtherMaterielOverviewListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mMaterielList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mMaterielList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_other_select_supplier_list_first_item, null);
            EditText materielCode = (EditText) convertView.findViewById(R.id.supplierCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            materielCode.setText(mMaterielCode);
            materielCode.setHint(mContext.getResources().getString(R.string.activity_inventory_select_materiel_adapter_please_input_material_code));
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mInStorageOtherOverviewListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mInStorageOtherOverviewListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            InStorageOtherOverviewBean.MaterielListBean materielItem = mMaterielList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_other_materiel_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.inStorageNumber = (EditText) view.findViewById(R.id.inStorageNumber);
        viewHolder.batch = (TextView) view.findViewById(R.id.batch);
        viewHolder.unit = (TextView) view.findViewById(R.id.unit);
        viewHolder.retainDecimal = (TextView) view.findViewById(R.id.retainDecimal);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InStorageOtherOverviewBean.MaterielListBean otherInStorageItem) {
        viewHolder.materielCode.setText(otherInStorageItem.getMat_code());
        viewHolder.materielDescribe.setText(otherInStorageItem.getMat_name());
        viewHolder.inStorageNumber.setText(TextUtils.isEmpty(otherInStorageItem.getInStorageNumber()) ? "" : otherInStorageItem.getInStorageNumber());
        viewHolder.retainDecimal.setText(otherInStorageItem.getDecimal_place());
        viewHolder.unit.setText(otherInStorageItem.getName_zh());
        boolean flag = InStorageUtils.hasOtherBatchRequired(otherInStorageItem.getBatch_material_spec_list() , otherInStorageItem.getBatch_spec_list());
        if (flag) {
            String batchInfo = mContext.getResources().getString(R.string.adapter_in_storage_item_list_batch_required);
            SpannableStringBuilder builder = new SpannableStringBuilder(batchInfo);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
            builder.setSpan(redSpan, batchInfo.length() - 1, batchInfo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            viewHolder.batch.setText(builder);
        } else {
            viewHolder.batch.setText(mContext.getResources().getString(R.string.adapter_in_storage_item_list_batch_not_required));
        }
//        int flag = InStorageUtils.hasOtherBatchInfo(materielItem.getBatch());
//        switch (flag) {
//            case InStorageUtils.NO_BATCH:
//                viewHolder.batchLayout.setVisibility(View.GONE);
//                break;
//            case InStorageUtils.BATCH_NO_MESSAGE:
//                break;
//            case InStorageUtils.BATCH_HAS_MESSAGE:
//                viewHolder.batch.setText("修改");
//                viewHolder.batchLayout.setVisibility(View.VISIBLE);
//                break;
//            case InStorageUtils.BATCH_NO_MESSAGE_BUT_REQUIRED:
//                viewHolder.batch.setText("必填");
//                viewHolder.batchLayout.setVisibility(View.VISIBLE);
//                break;
//        }
        if (InStorageUtils.checkOtherItem(otherInStorageItem)) {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }
        viewHolder.materielDescribe.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (mInStorageOtherOverviewListener.onCheckData()) {
                    Intent intent = new Intent(context, InStorageOtherMaterielDetailActivity.class);
                    intent.putExtra(InStorageExtra.MATERIEL_CODE, otherInStorageItem.getMat_code());
                    context.startActivity(intent);
                }
            }
        });
        view.setOnLongClickListener((View v) -> {
            mInStorageOtherOverviewListener.deleteMaterielItem(otherInStorageItem.getMat_code());
            return true;
        });
    }

    private class ViewHolder {
        private TextView materielCode, materielDescribe, unit, batch, retainDecimal;

        private EditText inStorageNumber;

        private PercentRelativeLayout headLayout;
    }

}
